package de.idgard.android.addon.ui.services.impl;

import java.util.List;

import android.webkit.WebSettings;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
import de.idgard.android.addon.ui.services.AddonStateService;
import de.idgard.android.addon.ui.services.BrowserPrivateModeService;
import de.idgard.android.addon.ui.services.TransparencyStateService;
import de.idgard.android.addon.ui.services.ZircoToIDGardAPI;

/**
 * 
 * @author ckaratza
 * 
 */

class BrowserPrivateModeServiceImpl implements BrowserPrivateModeService {

	ZircoToIDGardAPI api;

	public void setZircoToIDGardAPI(ZircoToIDGardAPI api) {
		this.api = api;
	}

	private TransparencyStateService transparencyStateService;
	private AddonStateService addonStateService;

	public void setTransparencyStateService(
			TransparencyStateService transparencyStateService) {
		this.transparencyStateService = transparencyStateService;
	}

	public void setAddonStateService(AddonStateService addonStateService) {
		this.addonStateService = addonStateService;
	}

	@Override
	public void configureBrowserPrivateMode(final WebView wview) {
    
		// TODO: Android 4+ supports privacy mode in webview...
		WebSettings settings = wview.getSettings();
		settings.setAppCacheEnabled(true);
		settings.setAllowFileAccess(true);
		settings.setJavaScriptCanOpenWindowsAutomatically(true);

		// settings.setNavDump(!addonStateService.isAddonON());
		settings.setSavePassword(!addonStateService.isAddonON());
		settings.setSaveFormData(!addonStateService.isAddonON());
		settings.setPluginState(transparencyStateService.isIncognito() ? PluginState.OFF
				: PluginState.ON);
	}

	@Override
	public List<WebView> configureBrowserPrivateModeForAllViews() {
		List<WebView> views = api.getContainersWebViews();
		if (views != null) {
			for (WebView view : views) {
				configureBrowserPrivateMode(view);
			}
		}
		return views;
	}

}
